package cap.action;

import cap.bean.ChatVo;
import cap.dao.ChatDao;
import cap.dao.impl.ChatDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/speaking")
public class speakingServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf8");
        ChatDao chatDao = new ChatDaoImpl();
        ChatVo chatVo = new ChatVo();
        HttpSession session = request.getSession();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        chatVo.setSpeaking(request.getParameter("speaking")+"------"+format.format(new Date()));
        chatVo.setSpeaker(session.getAttribute("nickname").toString());
        chatVo.setRoom((Integer) session.getAttribute("room"));
        try{
            chatDao.speaking(chatVo);
        }catch (Exception e){
            e.printStackTrace();
        }
        response.sendRedirect("front/chatting.jsp");
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
